import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

import '../constants.dart';

class RestaurantAds extends StatelessWidget {
  const RestaurantAds();
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.all(margin10),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10),
        child: Container(
          color: Colors.white,
          child: Stack(
            children: [
              Container(
                width: double.infinity,
                height: 80,
                child: CachedNetworkImage(imageUrl: urlSpecialRecommendedHeader,
                  fit: BoxFit.fitWidth,),
              ),
              Container(
                padding: const EdgeInsets.fromLTRB(8, 36, 8, 8),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Container(
                          width: 50,
                          height: 50,
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(8),
                            child: Image.network(
                              urlSpecialRecommendedThumb,
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),
                        SizedBox(width: 8),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              "相约炸串",
                              style: TextStyle(color: Colors.white),
                            ),
                            Row(
                              children: [
                                for (String s in [
                                  "25减3",
                                  "45减5",
                                  "68减8",
                                  "88减10"
                                ])
                                  RedTag(title: s)
                              ],
                            ),
                          ],
                        ),
                      ],
                    ),
                    const Text(
                      "每天换新油，吃健康炸串",
                      style: TextStyle(
                        fontSize: 18,
                        height: 2,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        ...List.generate(specialRecommendedFoodList.length + specialRecommendedFoodList.length - 1, (index) {
                          if (index % 2 == 1) {
                            return SizedBox(width: 6);
                          } else {
                            return _FoodItem(data: specialRecommendedFoodList[index ~/ 2]);
                          }
                        })
                        // for (Map<String, String> item in data) recommendedRestaurantItem(item)
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class RedTag extends StatelessWidget {
  final String title;
  RedTag({Key key, this.title});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(right: 4),
      padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 1),
      decoration: BoxDecoration(
          color: Colors.redAccent, borderRadius: BorderRadius.circular(2)),
      child: Text(
        title,
        style: const TextStyle(
          color: Colors.white,
          fontSize: 10,
        ),
      ),
    );
  }
}

class _FoodItem extends StatelessWidget {
  final Map<String, String> data;
  const _FoodItem({Key key, this.data}): super(key: key);

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(8),
            child: Container(
              width: double.infinity,
              height: 80,
              child: CachedNetworkImage(imageUrl: data['image'], fit: BoxFit.cover,),
            ),
          ),
          Text(data['title']),
          Row(
            children: [
              Text(
                "¥${data['price']}",
                style: TextStyle(color: Colors.red),
              ),
              SizedBox(width: 4),
              Text(
                "¥${data['discountPrice']}",
                style: TextStyle(
                  fontSize: 12,
                  color: Colors.grey,
                  decoration: TextDecoration.lineThrough,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

